package net.zjitc.logincheckcode.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @Author Administrator
 * @Date 2020/6/9 15:48
 * JDBC工具类:利用Druid
 **/
public class JDBCUtils {
    private static DataSource ds;
    static {
       /* 1、读取配置文件
       *  2、初始化ds*/
        try {
            Properties pro=new Properties();
            /* 传入参数：通过类加载器将文件转成流对象*/
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            ds= DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回数据源
     * @return
     */
    public static DataSource getDataSoutce(){
        return ds;
    }

    /**
     * 返回数据库连接对象
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

}
